﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Communication;
using Core;

namespace Service.Agent
{
    class ForwarderCommunication
    {
        internal static int AddEntity(Entity entity)
        {
            int result = -1;
            var q = from el in MainService.EntityList
                    where el.Name == entity.Name &&
                            el.EntityTypeId == entity.EntityTypeId &&
                            el.ParentId == entity.ParentId &&
                            el.TopLevelId == entity.TopLevelId
                    select el;
            if (q.Any())
            {
                result = q.FirstOrDefault().Id;

                Logging.LogInformation("Entity already cached with Id: " + result.ToString());
            }
            else
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    result = a.AddEntity(entity);
                    if (result != -1)
                    {
                        entity.Id = result;
                        MainService.EntityList.Add(entity);
                    }

                    Logging.LogInformation("Result from AddEntity: " + result.ToString());
                    (a as ICommunicationObject).Close();
                }
            }
            return result;
        }

        
        internal static List<EntityType> GetEntityTypeList()
        {
            List<EntityType> resultSet = null;
            IMonitoring a = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    a = scf.CreateChannel();
                    resultSet = a.GetEntityTypeList();

                    Logging.LogInformation("Entitytype list retrieved");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            if ((a as ICommunicationObject).State == CommunicationState.Opened) (a as ICommunicationObject).Close();
            return resultSet;
        }

        internal static List<EntityTypeProperty> GetEntityTypePropertyList()
        {
            List<EntityTypeProperty> resultSet = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    //MainService.EntityTypePropertyList = a.GetEntityTypePropertyList();
                    resultSet = a.GetEntityTypePropertyList();

                    Logging.LogInformation("Entitytypeproperty list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            return resultSet;
        }

        internal static List<AlertRule> GetAlertRuleList()
        {
            List<AlertRule> resultSet = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    //MainService.AlertRuleList = a.GetAlertRuleList();
                    resultSet = a.GetAlertRuleList();

                    Logging.LogInformation("AlertRule list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            return resultSet;
        }

        internal static List<dimDate> GetDateList()
        {
            List<dimDate> resultSet = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    resultSet = a.GetDateList();

                    Logging.LogInformation("AlertRule list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            return resultSet;
        }

        internal static List<dimPerfMonCounter> GetPerfMonCounterList()
        {
            List<dimPerfMonCounter> resultSet = null;
            IMonitoring a = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    //IMonitoring a = null;
                    a = scf.CreateChannel();
                    resultSet = a.GetPerfMonCounterList();

                    Logging.LogInformation("AlertRule list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());

                Logging.LogInformation("AlertRule list refreshed");
                resultSet = new List<dimPerfMonCounter>();
                //(a as ICommunicationObject).Close();
            }
            return resultSet;
        }

        internal static List<dimPerfMon> GetPerfMonList()
        {
            List<dimPerfMon> resultSet = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    resultSet = a.GetPerfMonList();

                    Logging.LogInformation("AlertRule list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            return resultSet;
        }

        internal static List<DeviationPercent> GetDeviationPercentList()
        {
            List<DeviationPercent> resultSet = null;
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    resultSet = a.GetDeviationPercentList();

                    Logging.LogInformation("AlertRule list refreshed");
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
            return resultSet;
        }

        internal static int AddEntityProperty(EntityProperty entityProperty)
        {
            int result = -1;
            var q = from epl in MainService.EntityPropertyList
                    where epl.Entity.Id == entityProperty.Entity.Id && epl.EntityTypeProperty.Id == entityProperty.EntityTypeProperty.Id
                    select epl;
            if (q.Any())
            {
                result = q.FirstOrDefault().Id;
            }
            else
            {
                try
                {
                    using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                    {
                        IMonitoring a = null;
                        a = scf.CreateChannel();

                        result = a.AddEntityProperty(entityProperty);
                        if (result != -1)
                        {
                            entityProperty.Id = result;
                            MainService.EntityPropertyList.Add(entityProperty);
                        }
                        (a as ICommunicationObject).Close();
                    }
                }
                catch (Exception ex)
                {
                    Logging.LogError(ex.ToString());
                }
            }
            return result;
        }

        internal static void AddAlert(AlertRule alertRule, Entity entity, EntityProperty entityProperty)
        {
            try
            {
                using (ChannelFactory<IMonitoring> scf = new ChannelFactory<IMonitoring>("defaultEndpoint"))
                {
                    IMonitoring a = null;
                    a = scf.CreateChannel();
                    a.AddAlert(alertRule, entity, entityProperty);
                    (a as ICommunicationObject).Close();
                }
            }
            catch (Exception ex)
            {
                Logging.LogError(ex.ToString());
            }
        }
    }
}

